
#ifndef _COMMUNKIT_H_
#define _COMMUNKIT_H_

#include <QObject>
#include <QString>
#include <QMap>
#include <queue>
#include <list>
#include "../management/channel.h"
#include "../management/message_struct.pb.h"

#define RETURN_VOID(str) do{sendReturnJson(str, 0, 0, "");} while(0)

class InfoConstructor;
class CommunKit : public QObject {
    Q_OBJECT
    public:
    CommunKit(ProcessRouter::Channel *channel);
    ~CommunKit();

    std::list<std::string> recvNoWait();
    void sendReturnJson(QString clientid, long pageid, long funcid, QString retval, bool encode = true);
    void sendDestroyMessage();
    void uploadState();
    void updateProxy();
    void activationProtocol();
    QString protocol();
    std::string getStateInfo();

    bool Base64Encode(const std::string& input, std::string* output);
    bool Base64Decode(const std::string& input, std::string* output);
    void destroy();

    void putRequestQueue(std::string msg);
    void sendStateInfo();
    void sendPageInfo();

    ProcessRouter::Channel* channel() { return m_channel; }

    protected:
        void timerEvent(QTimerEvent *event);

    private:
    bool getAvaliablePort(unsigned short &port);
    std::string listToJson(QList<QString> &list);

    //clientid : address
    std::queue<std::string> m_msgs;
    std::string m_protocol;
    ProcessRouter::CoreStatus m_status;
    ProcessRouter::CorePages m_pages;
    ProcessRouter::Channel *m_channel;
    bool m_hasActive;
    int m_timerId;
    boost::mutex m_mutex;
};

#endif

